package com.mr.cai.main.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cai.mr.lib.utils.LogInfo;
import com.mr.cai.main.R;
import com.mr.cai.main.activity.ClassifyDetailActivity;
import com.mr.cai.main.adapter.ClassifyContentAdapter;

/**
 * Created by Administrator on 16-2-25.
 */
public class ClassifyContentFragment extends BaseFragment {
    private RecyclerView mRecycleView;
    private View mView;
    private ClassifyContentAdapter adapter;
    private static final String TAG=ClassifyContentFragment.class.getSimpleName();

    public static BaseFragment newInstance(){
        ClassifyContentFragment fragment=new ClassifyContentFragment();
        return fragment;
    }



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogInfo.log(TAG, "onCreate");
        mView=LayoutInflater.from(getActivity()).inflate(R.layout.classify_content_fg_layout,null);
        findView();
        setListener();
    }

    private void setListener() {
        adapter.setOnItemClickLitener(new ClassifyContentAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent=new Intent(getActivity(),ClassifyDetailActivity.class);
                getActivity().startActivity(intent);
            }
        });
    }

    private void findView() {
        mRecycleView=(RecyclerView)mView.findViewById(R.id.contentRecycleViewLv);
        mRecycleView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        initData();
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        LogInfo.log(TAG, "onCreateView");
        return mView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);
    }


    private void initData() {
        adapter=new ClassifyContentAdapter(getActivity());
        mRecycleView.setAdapter(adapter);
    }

    @Override
    public void onDestroyView() {
        LogInfo.log(TAG,"onDestroyView");

        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        if(adapter!=null){
            adapter.clearData();
            adapter.onDetachedFromRecyclerView(mRecycleView);
        }
        if(mRecycleView!=null){
            mRecycleView.removeAllViews();
            mRecycleView.removeAllViewsInLayout();
            mRecycleView=null;
        }
        mView=null;
        adapter=null;
        LogInfo.log(TAG,"onDestroy");
        System.gc();
        super.onDestroy();
    }
}
